﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Linq;

namespace DienThoai
{
    /// <summary>
    /// Interaction logic for StoreKeeper.xaml
    /// </summary>
    public partial class StoreKeeper : Window
    {
        DienThoaiDataContext db = new DienThoaiDataContext();
        public string UserName;
        public StoreKeeper()
        {
            this.InitializeComponent();

            // Insert code required on object creation below this point.
        }

        private void btTK_Click(object sender, RoutedEventArgs e)
        {
            if (cbbLoaiTk.SelectedIndex == 0)
            {
                if (txtTK.Text == "")
                {
                    var query = from i in db.BuyProducts
                                from j in db.BuyPruductDetails
                                from k in db.Products
                                where k.ProductID == j.ProductID && j.BuyProductID == i.BuyProductID
                                select i;
                    dgListHD.ItemsSource = query.ToList();
                }
                else
                {
                    var query = from i in db.BuyProducts
                            from j in db.BuyPruductDetails
                            from k in db.Products
                            where k.ProductID == j.ProductID && j.BuyProductID == i.BuyProductID && j.ProductID==txtTK.Text
                            select i;
                    dgListHD.ItemsSource = query.ToList();
                }
            }
            else
                if (cbbLoaiTk.SelectedIndex == 1)
                {
                    if (txtTK.Text == "")
                    {
                        var query = from t in db.BuyProducts
                                    select t;
                        dgListHD.ItemsSource = query.ToList();
                    }
                    else
                    {
                        var query = from t in db.BuyProducts
                                    where t.BuyProductID == txtTK.Text
                                    select t;
                        dgListHD.ItemsSource = query.ToList();
                    }
                }
                else
                    if (cbbLoaiTk.SelectedIndex == 2)
                    {
                        if (txtTK.Text == "")
                        {
                            var query = from t in db.BuyProducts
                                        where t.SupplierID == t.Supplier.SupplierID
                                        select t;
                            dgListHD.ItemsSource = query.ToList();
                        }
                        else
                        {
                            var query = from t in db.BuyProducts
                                        where t.Supplier.SupplierID == txtTK.Text
                                        select t;
                            dgListHD.ItemsSource = query.ToList();
                        }
                    }
                        
        }

        private void dgListHD_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BuyProduct a = dgListHD.SelectedItem as BuyProduct;
            lbMaHD.Content = a.BuyProductID;
            lbNPP.Content = a.Supplier.Name;
            lbNgayLap.Content = a.BuyDate.ToString();
            var query = from t in db.BuyPruductDetails
                        join t1 in db.Products on t.ProductID equals t1.ProductID
                        join t2 in db.Manufacrurers on t1.ManufacturerID equals t2.ManufacrurerID
                        join t3 in db.BuyProducts on t.BuyProductID equals t3.BuyProductID
                        where t.BuyProductID == a.BuyProductID
                        select t;
            dgHD.ItemsSource = query;
            lbTongTien.Content = query.Single().BuyProduct.Amount;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var query = from t in db.Suppliers
                        select t.Name;
            foreach (var item in query)
            {
                cbbNPP.Items.Add(item);
            }
            query = from t in db.Manufacrurers
                    select t.ManufacrurerName;
            foreach (var item in query)
            {
                cbbHangSX.Items.Add(item);
            }
            cbbLoaiTk.SelectedIndex = 0;
        }

        private bool  KiemTraNhap()
        {
            if (cbbNPP.SelectedValue == null)
            {
                MessageBox.Show("Chưa chọn nhà phân phối");
                return false;
            }
            else
                if (cbbHangSX.SelectedValue == null)
                {
                    MessageBox.Show("Chưa chọn hãng sản xuất");
                    return false;
                }
                else
                    if (cbbTenHang.SelectedValue == null)
                    {
                        MessageBox.Show("Chưa chọn tên sản phẩm");
                        return false;
                    }
                    else
                        if (txtDonGia.Text == "")
                        {
                            MessageBox.Show("Chưa nhập đơn giá");
                            return false;
                        }
                        else
                            if (txtSoLuong.Text == "")
                            {
                                MessageBox.Show("Chưa nhập số lượng");
                                return false;
                            }
            return true;
        }
        private double tongtien=0;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (KiemTraNhap() == false)
            {
                return;
            }
            else
            {
                BuyProduct newBuyProduct = new BuyProduct();
                BuyPruductDetail newBuyProductDetail = new BuyPruductDetail();

                if (dg_New_HD.Items.Count == 0)
                {
                    newBuyProduct.BuyProductID = getID();
                }
                lb_New_MaHD.Content = newBuyProduct.BuyProductID;
                newBuyProductDetail.BuyProductID = newBuyProduct.BuyProductID;
                var query = from t in db.Products
                            where t.Model == cbbTenHang.SelectedItem.ToString() && t.Manufacrurer.ManufacrurerName == cbbHangSX.SelectedItem.ToString()
                            select t.ProductID;
                newBuyProductDetail.ProductID = query.Single();

                newBuyProductDetail.Price = float.Parse(txtDonGia.Text);
                newBuyProductDetail.Quantity = int.Parse(txtSoLuong.Text);

                double i = 0;
                i = float.Parse(txtDonGia.Text) * int.Parse(txtSoLuong.Text);
                tongtien += i;
                dg_New_HD.Items.Add(newBuyProductDetail);
                //cbbHangSX.SelectedItem = null;
                //cbbTenHang.SelectedItem = null;
                txtDonGia.Text = "";
                txtSoLuong.Text = "";
                lb_New_TT.Content = tongtien.ToString();
            }
        }
        private string getID()
        {
            string ID = "";

            var query = from i in db.BuyProducts
                        select i.BuyProductID;

            if (query.Count() == 0)
                ID = "HD00001";
            else
            {
                int temp = 0;
                foreach (var i in query)
                    temp = int.Parse(i.ToString().Substring(2)) + 1;
                if (temp < 10)
                    ID = "HD0000" + temp.ToString();
                else if (temp < 100)
                    ID = "HD000" + temp.ToString();
                else if (temp < 1000)
                    ID = "HD00" + temp.ToString();
                else if (temp < 10000)
                    ID = "HD0" + temp.ToString();
                else
                    ID = "HD" + temp.ToString();
            }
            return ID;
        }

        

        private void TabItem_GotFocus(object sender, RoutedEventArgs e)
        {
            lb_New_NgayLap.Content = DateTime.Today.ToString();
        }

        private void cbbHangSX_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Utility util = new Utility();
            util.ClearCombobox(cbbTenHang);
            var query = from t in db.Products
                        where t.Manufacrurer.ManufacrurerName == cbbHangSX.SelectedItem.ToString()
                        select t.Model;
            foreach (var item in query)
            {
                cbbTenHang.Items.Add(item);
            }
        }
        
        private void ClearDataGrid(DataGrid dg)
        {
            int t = dg.Items.Count;
            for (int i = t-1; i >=0; i--)
            {
                dg.Items.RemoveAt(i);
            }
        }

        private bool CheckNhapHoaDon()
        {
            if (dg_New_HD.Items.Count == 0)
            {
                MessageBox.Show("Chưa có sản phẩm nào được nhập");
                return false;
            }
            else
                return true;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (CheckNhapHoaDon() == false)
                return;
            else
            {

                var query = from t in db.Suppliers

                            where t.Name == cbbNPP.SelectedValue.ToString()
                            select t.SupplierID;
                BuyProduct newBuyProduct = new BuyProduct();
                newBuyProduct.BuyProductID = lb_New_MaHD.Content.ToString();
                newBuyProduct.SupplierID = query.Single();
                newBuyProduct.BuyDate = DateTime.Today;
                newBuyProduct.Amount = double.Parse(lb_New_TT.Content.ToString());
                db.BuyProducts.InsertOnSubmit(newBuyProduct);
                BuyPruductDetail detail = new BuyPruductDetail();
                foreach (var item in dg_New_HD.Items)
                {
                    detail.BuyProductID = lb_New_MaHD.ToString();
                    detail = item as BuyPruductDetail;
                    db.BuyPruductDetails.InsertOnSubmit(detail);
                    var query1 = from t in db.Products
                                 where t.ProductID == detail.ProductID
                                 select t;
                    foreach (var item1 in query1)
                    {
                        item1.Quantity += detail.Quantity;
                        db.SubmitChanges();
                    }

                }



                db.SubmitChanges();
                MessageBox.Show("Tạo hóa đơn thành công.");
                ClearDataGrid(dg_New_HD);
                tongtien = 0;
            }
        }

        private void btHuy_Click(object sender, RoutedEventArgs e)
        {
            dg_New_HD.Items.Remove(dg_New_HD.SelectedItem);
            tongtien = 0;
        }

        private void btHuyTC_Click(object sender, RoutedEventArgs e)
        {
            if (dg_New_HD.Items.Count == 0)
                return;
            else
            {
                int t = dg_New_HD.Items.Count;
                for (int i = t - 1; i >= 0; i--)
                {
                    dg_New_HD.Items.RemoveAt(i);
                }
            }
            tongtien = 0;
        }

        private void btThoat_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
    }
}